package testJdk.thread.pool.testThreadPool;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义任务类
 * 任务和线程是不同的类，任务在线程中执行
 */
public class MyTask implements Runnable {
    private final Logger logger = LoggerFactory.getLogger(MyTask.class);

    private final int id;

    public MyTask(int id) {
        this.id = id;
    }

    @SneakyThrows
    @Override
    public void run() {
        // 获取执行该任务的当前线程
        Thread thisThread = Thread.currentThread();
        String thisThreadName = thisThread.getName();

        logger.info("线程-" + thisThreadName + "-即将执行任务：{}", id);
        Thread.sleep(200);
        logger.info("线程-" + thisThreadName + "-完成任务：{}", id);
    }

    @Override
    public String toString() {
        return "MyTask{" + "id=" + id + '}';
    }
}
